﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IService.Model;
using IService.Utility;

namespace IService.User_center
{
    public partial class uc_1_1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //try
            //{
            //    if (Convert.ToInt32(base.Request["RequestType"])==1)
            //    {
            //        string str = string.Format("<script language=javascript></script>");
            //        Response.Write("");
            //    }
            //}
        }

        /// <summary>
        /// 供应信息提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSupply_Click(object sender, EventArgs e)
        {
            this.lblErrorSupply.Text = "";
            //获取选择的checkbox
            string chkSelect = "";
            for (int i = 0; i < this.cblSupplyProductType.Items.Count; i++)
            {
                if (this.cblSupplyProductType.Items[i].Selected == true)
                    chkSelect += this.cblSupplyProductType.Items[i].Value + ",";
            }
            if (chkSelect != "")
                chkSelect = chkSelect.Substring(0, chkSelect.Length - 1);

            string productType = chkSelect;
            string productName = this.txtSupplyProductName.Text.Trim();
            string infoTitle = this.txtSupplyInfoTitle.Text.Trim();
            string productDetail = Server.HtmlEncode(this.txtareaSupplyProductDetailt.Value);
            //检查信息是否完整
            if (string.IsNullOrEmpty(productType))
            {
                this.lblErrorSupply.Text = "<font color='red'>请选择产品类型！</font>";
            }
            else if (string.IsNullOrEmpty(productName))
            {
                this.lblErrorSupply.Text = "<font color='red'>请输入名称！</font>";
            }
            else if (string.IsNullOrEmpty(infoTitle))
            {
                this.lblErrorSupply.Text = "<font color='red'>清输入信息标题！</font>";
            }
            else
            {
                //初始化对象
                SupplyPurchase info = new SupplyPurchase();
                info.ProductType = productType;
                info.ProductName = productName;
                info.InfoTitle = infoTitle;
                info.ProductDetail = productDetail;
                info.UserID = SessionState.CurrentUser.ID;
                info.Type = 1;
                info.RequestType = RequestType;
                info.CreateDate = DateTime.Now;
                info.UpdateDateTime = DateTime.Now;
                info.ProductPicture = "";
                info.ProductVideo = "";
                //开始保存对象到数据库中
                bool hassuccess = info.Save();
                //保存成功
                if (hassuccess)
                {
                    JsUtil.Alert("信息提交成功！", this.Page);
                    this.txtSupplyProductName.Text = "";
                    this.txtSupplyInfoTitle.Text = "";
                    this.txtareaSupplyProductDetailt.Value = "";
                    for (int i = 0; i < this.cblSupplyProductType.Items.Count; i++)
                    {
                        this.cblSupplyProductType.Items[i].Selected = false;
                    }
                    this.hdImgProduct1.Value = "";
                    this.hdImgProduct2.Value = "";
                    this.hdImgProduct3.Value = "";
                }
                else
                {
                    //保存失败
                    JsUtil.Alert("信息提交失败！", this.Page);
                }
            }
        }

        /// <summary>
        /// 采购信息提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPurchase_Click(object sender, EventArgs e)
        {
            this.lblErrorPurchase.Text = "";
            //获取选择的checkbox
            string chkSelect = "";
            for (int i = 0; i < this.cblPurchaseProductType.Items.Count; i++)
            {
                if (this.cblPurchaseProductType.Items[i].Selected == true)
                    chkSelect += this.cblPurchaseProductType.Items[i].Value + ",";
            }
            if (chkSelect != "")
                chkSelect = chkSelect.Substring(0, chkSelect.Length - 1);

            string productType = chkSelect;
            string productName = this.txtPurchaseProductName.Text.Trim();
            string infoTitle = this.txtPurchaseInfoTitle.Text.Trim();
            string productDetail = Server.HtmlEncode(this.txtareaPurchaseProductDetail.Value);
            //检查信息是否完整
            if (string.IsNullOrEmpty(productType))
            {
                this.lblErrorPurchase.Text = "<font color='red'>请选择产品类型！</font>";
            }
            else if (string.IsNullOrEmpty(productName))
            {
                this.lblErrorPurchase.Text = "<font color='red'>请输入名称！</font>";
            }
            else if (string.IsNullOrEmpty(infoTitle))
            {
                this.lblErrorPurchase.Text = "<font color='red'>清输入信息标题！</font>";
            }
            else
            {
                //初始化对象
                SupplyPurchase info = new SupplyPurchase();
                info.ProductType = productType;
                info.ProductName = productName;
                info.InfoTitle = infoTitle;
                info.ProductDetail = productDetail;
                info.UserID = SessionState.CurrentUser.ID;
                info.Type = 2;
                info.RequestType = RequestType;
                info.CreateDate = DateTime.Now;
                info.UpdateDateTime = DateTime.Now;

                //获取上传的图片URL
                string strPic = "";
                if (!string.IsNullOrEmpty(hdImgProduct1.Value))
                {
                    strPic += hdImgProduct1.Value + '&';
                }
                if (!string.IsNullOrEmpty(hdImgProduct2.Value))
                {
                    strPic += hdImgProduct2.Value + '&';
                }
                if (!string.IsNullOrEmpty(hdImgProduct3.Value))
                {
                    strPic += hdImgProduct3.Value;
                }
                info.ProductPicture = strPic;

                //获取上传视频的URL
                string strVideo = "";
                if (!string.IsNullOrEmpty(hdImgProduct4.Value))
                {
                    strVideo += hdImgProduct4.Value + '&';
                }
                if (!string.IsNullOrEmpty(hdImgProduct5.Value))
                {
                    strVideo += hdImgProduct5.Value + '&';
                }
                if (!string.IsNullOrEmpty(hdImgProduct6.Value))
                {
                    strVideo += hdImgProduct6.Value;
                }
                info.ProductVideo = strVideo;

                //开始保存对象到数据库中
                bool hassuccess = info.Save();
                //保存成功
                if (hassuccess)
                {
                    JsUtil.Alert("信息提交成功！", this.Page);
                    this.txtPurchaseProductName.Text = "";
                    this.txtPurchaseInfoTitle.Text = "";
                    this.txtareaPurchaseProductDetail.Value = "";
                    for (int i = 0; i < this.cblPurchaseProductType.Items.Count; i++)
                    {
                        this.cblPurchaseProductType.Items[i].Selected = false;
                    }
                    this.hdImgProduct4.Value = "";
                    this.hdImgProduct5.Value = "";
                    this.hdImgProduct6.Value = "";
                }
                else
                {
                    //保存失败
                    JsUtil.Alert("信息提交失败！", this.Page);
                }
            }
        }

        protected int RequestType
        {
            get
            {
                try
                {
                    return Convert.ToInt32(base.Request["RequestType"]);
                }
                catch
                {
                    return 1;
                }
            }
        }
    }
}